package com.vhall.component.controller.export;

import com.alibaba.fastjson.JSON;
import com.vhall.component.controller.export.facade.ExportFacade;
import com.vhall.component.entity.data.vo.DataExportParamVO;
import com.vhall.component.entity.data.vo.DataExportRspVO;
import com.vhall.component.entity.export.ExportEntity;
import com.vhall.component.entity.export.vo.ExportListReqVO;
import com.vhall.component.entity.export.vo.ExportListRspVO;
import com.vhall.component.entity.export.vo.ExportPage;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.service.export.ExportService;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author guoxin.wang
 * @date 2021-06-15 14:00
 */
@VhallComponent
public class ExportComponent implements ExportFacade {

    @Autowired
    private ExportService exportService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public ExportPage<ExportListRspVO> exportList(ExportListReqVO reqVO) {
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPageSize() == null) {
            reqVO.setPageSize(10);
        }
//        if (StringUtils.isNotBlank(reqVO.getEndTime())) {
//            String endTime = reqVO.getEndTime();
//            if (endTime.contains(ConstStr.TIME_ZERO)) {
//                endTime = endTime.replace(ConstStr.TIME_ZERO, ConstStr.TIME_FULL);
//            } else {
//                endTime = endTime + " " + ConstStr.TIME_FULL;
//            }
//            reqVO.setEndTime(endTime);
//        }
        assemblyAccountId(reqVO);
        reqVO.setEndTime(null);
        reqVO.setBeginTime(null);
        return exportService.exportList(reqVO);
    }

    @Override
    public Boolean createExport(ExportEntity ee) {
        return exportService.createExport(ee);
    }

    @Override
    public void whiteAccountTemplate() throws Exception {
        exportService.whiteAccountTemplate(response);
    }

    @TokenAuth
    @Override
    public List<DataExportRspVO> getAllInfo(DataExportParamVO reqVO) {
        return exportService.getAllInfo(reqVO);
    }

    @TokenAuth
    @Override
    public Integer getAllInfoNum(DataExportParamVO reqVO) {
        return exportService.getAllInfoNum(reqVO);
    }

    @Override
    public void lotteryTemplate() throws Exception {
        exportService.lotteryTemplate(response);
    }

    @Override
    public void filterTemplate() throws Exception {
        exportService.filterTemplate(response);
    }

    @Override
    public String getFileName(String roomName, Integer type, String exportTypeName) {
        return exportService.getFileName(roomName, type, exportTypeName);
    }

    private void assemblyAccountId(ExportListReqVO reqVO) {
        String token = reqVO.getToken();
        Preconditions.checkArgument(StringUtils.isNotBlank(token), "token不能为空！");

        token = redisTemplate.opsForValue().get(RedisKey.AUTH_TOKEN + token);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(BizErrorCode.AUTH_ADM_TOKEN_INVALID);
        }

        String tokenStr = JSON.parse(token).toString();
        Token parseObject = JSON.parseObject(tokenStr, Token.class);
        if (parseObject != null) {
            reqVO.setAccountId(parseObject.getAccountId() == null ? 0 : parseObject.getAccountId());
        }
    }

}
